package com.hupu.jrs.data.remote

import com.hupu.jrs.api.forum.ForumApi
import com.hupu.jrs.data.ContentDataSource
import javax.inject.Inject
import rx.Observable
import rx.schedulers.Schedulers

/**
 * Created by Stark.Tony on 2018/6/3.
 */
class ContentRemoteDataSource @Inject
constructor(private val mForumApi: ForumApi) : ContentDataSource {

    override fun getThreadInfo(fid: String, tid: String): Observable<ThreadInfo> {
        return mForumApi.getThreadInfo(tid, fid, 1, "")
                .onErrorReturn { null }
                .subscribeOn(Schedulers.io())
    }

    override fun getReplies(fid: String, tid: String, page: Int): Observable<List<ThreadReply>> {
        return mForumApi.getThreadReplyList(tid, fid, page)
                .map { threadReplyData ->
                    if (threadReplyData != null && threadReplyData.status == 200) {
                        threadReplyData.result!!.list
                    } else null
                }
                .onErrorReturn { null }
                .subscribeOn(Schedulers.io())
    }

    override fun getLightReplies(fid: String, tid: String): Observable<List<ThreadReply>> {
        return mForumApi.getThreadLightReplyList(tid, fid)
                .map { threadLightReplyData ->
                    if (threadLightReplyData != null && threadLightReplyData.status == 200) {
                        threadLightReplyData.list
                    } else null
                }
                .onErrorReturn { null }
                .subscribeOn(Schedulers.io())
    }
}
